#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash mysql++-3.3.0.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1
if [[ "$1" == "$2" ]]; then echo "构建路径与安装路径需为不同路径，请输入正确的路径"; exit 1; fi

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
set -e
yum install -y wget tar environment-modules
#安装mysql
yum install -y mysql5 mysql5-devel
#安装mysql++
wget -t 10 -c -P $buildpath https://tangentsoft.com/mysqlpp/releases/mysql++-3.3.0.tar.gz
tar xf $buildpath/mysql++-3.3.0.tar.gz -C $buildpath && rm -rf $buildpath/mysql++-3.3.0.tar.gz
cd $buildpath/mysql++-3.3.0

./configure CC=clang CXX=clang++ --prefix=$installpath/AUGUSTUS/mysql++-3.3.0                                                                       

make -j$(nproc)
make -j$(nproc) install

cat>"$installpath/AUGUSTUS/mysql++-3.3.0/mysql1_modulefiles"<<EOF
#%Module1.0
conflict mysql1
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set mysql1 \$pwd
setenv mysql1 \$mysql1
prepend-path LD_LIBRARY_PATH \$mysql1/lib
prepend-path LD_LIBRARY_PATH /usr/lib64/mysql
prepend-path INCLUDE \$mysql1/include
EOF

rm -rf $buildpath/mysql++-3.3.0

